package com.mryao.hello;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {

    public final static String QUEUE_NAME = "rabbitMQ.test";

    public static void main (String[] args) throws IOException, TimeoutException {
        //获取工厂
        ConnectionFactory factory = new ConnectionFactory();
        //设置ip
        factory.setHost("192.168.231.132");
        factory.setUsername("admin");
        factory.setPassword("admin");
        //打开连接
        Connection connection = factory.newConnection();
        //打开通道
        Channel channel = connection.createChannel();
        //声明队列
        //b:是否持久化 b2:是否独占队列 b3:当所有消费者客户端连接断开时是否自动删除队列 map:其他参数
        channel.queueDeclare(QUEUE_NAME,false,false,false,null);
        //向队列发送消息
        String message = "hello rabbitMQ";
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
        System.out.println("Producer send '" + message + "'");
        //释放资源
        channel.close();
        connection.close();
    }
}
